package org.gui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.lang.reflect.Constructor;

import javax.swing.JFrame;
import javax.swing.JPanel;

import mvc.controller.MouseController;
import mvc.model.ModelCurve;
import mvc.view.ViewCurve;

import org.render.IRenderer;


public abstract class MainWindow extends JFrame { private static final long serialVersionUID = 1L;
	
	//---------------------------------------------------------------------- LAYOUT CONSTANTS
	public static int WIDTH = 800;
	public static int HEIGHT = 800;

	//---------------------------------------------------------------------- class vars
	protected JPanel[] _views = new JPanel[1];
	
	protected ModelCurve curve;
	
	//---------------------------------------------------------------------- constructors
	public MainWindow(String title, ModelCurve curve) {
		super(title);
		this.curve = curve;
		
		init();
	}
	
	private void init() {
		System.out.println("MainWindow > init..");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLayout(new GridLayout(2,2, 1, 1));
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setBackground(Color.BLACK);
	}
	
	protected void createView(Dimension viewDimension, String title, 
			Class<?> viewClass, IRenderer r, 
			Class<?> controllerClass, String controllerParam) {
		ViewCurve v = getViewInstance(viewClass, r, viewDimension, title);
		MouseController c = getControllerInstance(controllerClass, controllerParam, v);
		System.out.println("MainWindow > anti warning > mouseController:"+c);
		super.add(v, BorderLayout.NORTH);
	}

	private ViewCurve getViewInstance(Class<?> viewClass, IRenderer r,
			Dimension viewDimension, String title) {
		Object args[] = {curve, r, viewDimension, title};
		return (ViewCurve) getInstance(viewClass, args);
	}

	private MouseController getControllerInstance(Class<?> className, String s, ViewCurve v) {
		Object args[] = {curve, v, s};
		return (MouseController) getInstance(className, args);
	}
	
	private Object getInstance(Class<?> className, Object[] args) {
		try {
			Class<?> paramTypes[] = new Class[args.length];
			for(int i=0; i<args.length; i++) paramTypes[i] = args[i].getClass();
			
			//System.out.println(Arrays.toString(paramTypes));
			//System.out.println(Arrays.toString(className.getConstructors()));
			
			//Constructor k = className.getDeclaredConstructor(paramTypes);
			Constructor<?> k = className.getDeclaredConstructors()[0];
			//System.out.println("MainWindow > getInstance > k: "+k);
			return k.newInstance(args);
		} catch(Exception e) { 
			System.out.println("______________________________");
			System.out.println("ElicoideMainWIndow > createView > reflection exception");
			e.printStackTrace();
			System.out.println("______________________________");
		}
		return null;
	}	

}
